package com.run;

import com.model.User;

import javax.swing.*;
import java.sql.*;
import java.util.Objects;

public class UserRun {
    /**
     * 登录验证
     * @param con
     * @param user
     * @return
     * @throws Exception
     */
    public User login(Connection con,User user) throws Exception {
        User resultUser = new User();
        String sql = "select * from user where username=? and password=?";

        PreparedStatement pstmt = con.prepareStatement(sql);
        pstmt.setString(1,user.getUserName());
        pstmt.setString(2,user.getPassword());
        ResultSet resultSet = pstmt.executeQuery();

//        if(resultSet.next()) {
//            resultUser.setId(resultSet.getInt("id"));
//            resultUser.setUserName(resultSet.getString("userName"));
//            resultUser.setPassword(resultSet.getString("password"));
//        }
        while(resultSet.next()) {
            if(Objects.equals(resultSet.getString("username"),user.getUserName()) &&
                    Objects.equals(resultSet.getString("password"),user.getPassword()) &&
                    Objects.equals(resultSet.getString("identity"),user.getIdentity())) {
                resultUser.setId(resultSet.getInt("id"));
                resultUser.setUserName(resultSet.getString("userName"));
                resultUser.setPassword(resultSet.getString("password"));
                break;
            }
        }
        return resultUser;
    }

    public boolean addUser(Connection con,String username,String password,String identity,String student_id) throws SQLException {
        if(identity == "manager") {
            String sql = "INSERT INTO user values (null,null,'" + username +"','" + password + "','" + identity + "')";
            Statement stmt = con.createStatement();
            int count = 0;
            count = stmt.executeUpdate(sql);
            if(count > 0) {
                System.out.println("注册成功");
                return true;
            }
            else {
                System.out.println("注册失败");
                return false;
            }
        }
        else if(identity == "user") {
            String sql = "INSERT INTO user values (null,'" + student_id + "','" + username +"','" + password + "','" + identity + "')";
            Statement stmt = con.createStatement();
            int count = 0;
            count = stmt.executeUpdate(sql);
            if(count > 0) {
                System.out.println("注册成功");
                return true;
            }
            else {
                System.out.println("注册失败");
                return false;
            }
        }

        return false;
    }

    public boolean deleteUser(Connection con,String username,String password,String identity) throws SQLException {
        String sql = "DELETE FROM user WHERE userName = '" + username + "' and password = '" + password + "' and identity = '" + identity + "'";
        Statement stmt = con.createStatement();
        int count = 0;
        count = stmt.executeUpdate(sql);
        if(count > 0) {
            System.out.println("删除成功");
            return true;
        }
        else {
            System.out.println("删除失败");
            return false;
        }
    }




    public boolean changePassword(Connection con,String username,String password,String identity,String newPassword) throws SQLException {
        String sql = "UPDATE user SET password = '"+ newPassword +"' WHERE username = '"+username+"' and password = '"+password+"' and identity = '"+identity+"'";
//        PreparedStatement pstmt = con.prepareStatement(sql);
//        pstmt.setString(1,newPassword);
//        pstmt.setString(2,username);
//        pstmt.setString(3,password);
//        pstmt.setString(4,identity);
        Statement stmt = con.createStatement();
        int count = 0;
//        count = pstmt.getUpdateCount();
        count = stmt.executeUpdate(sql);
        if(count > 0) {
            //System.out.println("密码已修改");
            return true;
        }
        else {
            //System.out.println("密码修改失败");
            return false;
        }
    }

    public ResultSet searchStudent(Connection con,String username) throws SQLException {
        String sql = "SELECT student_id FROM USER WHERE user.username = '" + username + "'";
        Statement stmt = con.createStatement();
        ResultSet resultSet = null;
        resultSet = stmt.executeQuery(sql);
        //String student_id = resultSet.getString(1);
        if(resultSet.first()) {
            resultSet.previous();
            return resultSet;
        }
        else {
            JOptionPane.showMessageDialog(null,"未找到该用户");
            return null;
        }

    }

}
